﻿using FarmSmartSleep.Common.Domain.PM;
using FarmSmartSleep.Common.Domain.RBACFile;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using FarmSmartSleep.Organ.API.Application.ProductionManagement.FemaleRen;
using MediatR;
using System.Reflection.Metadata;

namespace FarmSmartSleep.Organ.API.Application.ProductionManagement.FemaleRen
{
    public class FemaleRenUpdateHandler : IRequestHandler<FemaleRenUpdateCommand, Result>
    {
        private readonly IBaseRepository<EwePregnancyCheckRecord> ewePregnancyCheckRecordRepository;
        private readonly ILogger<FemaleRenUpdateHandler> logger;

        public FemaleRenUpdateHandler(IBaseRepository<EwePregnancyCheckRecord> ewePregnancyCheckRecordRepository, ILogger<FemaleRenUpdateHandler> logger)
        {
            this.ewePregnancyCheckRecordRepository = ewePregnancyCheckRecordRepository;
            this.logger = logger;
        }

        /// <summary>
        /// 修改母羊妊检信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(FemaleRenUpdateCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var result = new Result();
                var info = ewePregnancyCheckRecordRepository.GetAll().FirstOrDefault(x => x.EwePregnancyCheckRecordId == request.EwePregnancyCheckRecordId);
                if (info != null)
                {
                    info.EwePregnancyCheckRecordId = request.EwePregnancyCheckRecordId;//耳号
                    info.PregnancyTestResult = request.PregnancyTestResult;//妊检结果
                    info.PregnancyTestMethod = request.PregnancyTestMethod;//妊检方式
                    info.LitterCount = request.LitterCount;//同胎数
                    info.PregnancyDays = request.PregnancyDays;//妊振天数
                    info.CurrentBarn = request.CurrentBarn;//当前栋舍
                    info.CurrentPen = request.CurrentPen;//当前栏位
                    info.TransferBarn = request.TransferBarn;//转入栋舍
                    info.TransferPen = request.TransferPen;//转入栏位
                }
                result.Data = ewePregnancyCheckRecordRepository.Update(info);
                logger.LogInformation("修改母羊妊检信息方法调用成功");
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError("修改母羊妊检信息方法调用出错" + ex);
                throw;
            }
        }
    }
}
